﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Text;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// CommitteeMember 的摘要说明
    /// </summary>
    public class CommitteeMember:IRequiresSessionState,IHttpHandler
    {
        int CMUID = 0;
        int TypeUID = 0;
        int CommPositionUID = 0;
        int cid = 0;
        int CustomerUID = 0;
        DateTime startTime = new DateTime();
        DateTime entTime = new DateTime();
        tblCPerson_CommMemberBLL cp_commBLL = new tblCPerson_CommMemberBLL();
        tblCPerson_CommMember cp_commENT = new tblCPerson_CommMember();
        public void ProcessRequest(HttpContext context)
        {            
            string strRet = string.Empty;
            context.Response.ContentType = "text/plain";
            if (BasePage.GetInstance().IsCustomerLogin())
            {
                string optionType = context.Request.QueryString["OptionType"].ToString();
                int.TryParse(context.Request.QueryString["CMUID"], out CMUID);

                if (optionType == "get")
                {
                    strRet = GetCommENT(context);
                }
                else if (optionType == "update")
                {
                    strRet = UpdateCommENT(context);
                }
                else if (optionType == "add")
                {
                    strRet = AddCommENT(context);
                }
                else if(optionType=="del")
                {
                    strRet = DeleteCommENT(context);
                }
            }
            else
            {
                strRet = ResourceManage.GetResourceString("msg200009");
            }
            context.Response.Write(strRet);
            context.Response.Flush();
        }
        #region 获取信息
        private string GetCommENT(HttpContext context)
        {
            StringBuilder strBuilder = new StringBuilder();
            if (CMUID > 0)
            {
                cp_commENT.Cperson_CommMemberUID = CMUID;
                cp_commENT.Deleted = 0;
                cp_commENT = cp_commBLL.GetEntity(cp_commENT);
                strBuilder.Append("{\"TypeValue\":\"");
                strBuilder.Append(cp_commENT.CommTypeUID);
                strBuilder.Append("\",\"Position\":\"");
                strBuilder.Append(cp_commENT.CommPositionUID);
                strBuilder.Append("\",\"txtStartDate\":\"");
                if (cp_commENT.StartDate != null)
                {
                    strBuilder.Append(BasePage.GetInstance().DateFormat(cp_commENT.StartDate));
                }
                strBuilder.Append("\",\"txtEndDate\":\"");
                if (cp_commENT.EndDate != null) {
                    strBuilder.Append(BasePage.GetInstance().DateFormat(cp_commENT.EndDate));
                }
                strBuilder.Append("\",\"txtYear\":\"");
                strBuilder.Append(cp_commENT.Year);
                strBuilder.Append("\",\"txtRmark\":\"");
                strBuilder.Append(cp_commENT.Remarks);
                strBuilder.Append("\"}");
            }
            return strBuilder.ToString();
        }
        #endregion
        #region 修改信息
        private string UpdateCommENT(HttpContext context)
        {
            string str = string.Empty;
            cp_commENT = GetText(cp_commENT, context);
            int i= cp_commBLL.UpdateENT(cp_commENT);
            if (i > 0)
                str = ResourceManage.GetResourceString("msg200004");
            else
                str = ResourceManage.GetResourceString("msg200003");
            return str;
        }
        #endregion
        #region 添加信息
        private string AddCommENT(HttpContext context)
        {
            string str = string.Empty;
            cp_commENT = GetText(cp_commENT,context);
            //校验 Customer && CP是否存在关联
            tblCust_ContactPerson cust_cpENT = new tblCust_ContactPerson();
            tblCust_ContactPersonBLL cust_cpBLL = new tblCust_ContactPersonBLL();
            
            int.TryParse(context.Request.QueryString["CustomerUID"], out CustomerUID);
            cust_cpENT.CustomerUID = CustomerUID;
            cust_cpENT.ContactPersonUID = cp_commENT.ContactPersonUID;
            cust_cpENT.Deleted = 0;
            cust_cpENT.LastModifyDate = DateTime.Now;
            if (cust_cpBLL.IsExistCPInCustomer(cust_cpENT))
            {
                cust_cpENT.LastModifyBy = GetManageName(context);
                int i = cp_commBLL.InsertENT(cp_commENT);
                if (i > 0)
                    str = ResourceManage.GetResourceString("msg200006");
                else
                    str = ResourceManage.GetResourceString("msg200005");
            }
            else
            { str = ResourceManage.GetResourceString("msg200005"); }

            return str;
        }

        #endregion

        private string DeleteCommENT(HttpContext context)
        {
            string str = string.Empty;
            cp_commENT.Cperson_CommMemberUID = CMUID;
            var temp = cp_commBLL.GetEntity(cp_commENT);
            temp.Deleted = 1;
            temp.LastModifyBy = GetManageName(context);
            temp.LastModifyDate = DateTime.Now;
            int i = cp_commBLL.UpdateENT(temp);
            if (i > 0)
                str = ResourceManage.GetResourceString("msg200011");
            else
                str = ResourceManage.GetResourceString("msg200010");
            return str;
        }

        //实体赋值
        private tblCPerson_CommMember GetText(tblCPerson_CommMember ent,HttpContext context)
        {

            int.TryParse(context.Request.QueryString["TypeValue"], out TypeUID);
            int.TryParse(context.Request.QueryString["Position"], out CommPositionUID);
            int.TryParse(context.Request.QueryString["cid"], out cid);
            
            DateTime.TryParse(context.Request.QueryString["txtStartDate"],out startTime);
            DateTime.TryParse(context.Request.QueryString["txtEndDate"], out entTime);

            cp_commENT.ContactPersonUID = cid;
            cp_commENT.Cperson_CommMemberUID = CMUID;
            cp_commENT.CommTypeUID = TypeUID;
            cp_commENT.CommPositionUID = CommPositionUID;
            cp_commENT.StartDate = startTime;
            cp_commENT.EndDate = entTime;
            cp_commENT.Year = context.Request.QueryString["txtYear"].ToString();
            cp_commENT.Remarks = context.Request.QueryString["txtRmark"].ToString();
            return ent;
        }

        public string GetManageName(HttpContext context)
        {
            string ret = string.Empty;
            if (context.Session[Constant.SessionName.Manage] != null)
            {
                ret = (context.Session[Constant.SessionName.Manage] as tblUser).User_Name;
            }
            else
            { ret = "system"; }
            return ret;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}